//
//  CIFilterProcessor.m
//  vImageStudy
//
//  Created by Evan Xie on 8/7/15.
//  Copyright (c) 2015 Family Wings. All rights reserved.
//


#import <CoreImage/CoreImage.h>
#import "CIFilterProcessor.h"

@implementation CIFilterProcessor

+ (UIImage *)embossEffectForImage:(UIImage *)image
{
    if (!image) {
        return nil;
    }
    
    NSArray *filterCategories = @[kCICategoryDistortionEffect, kCICategoryGeometryAdjustment, kCICategoryCompositeOperation,
                                  kCICategoryHalftoneEffect, kCICategoryColorAdjustment, kCICategoryColorEffect, kCICategoryTransition,
                                  kCICategorySharpen, kCICategoryBlur, kCICategoryStylize, kCICategoryGradient, kCICategoryReduction, kCICategoryGenerator,
                                  kCICategoryTileEffect];
    
    for (NSString *filterCategory in filterCategories) {
        NSArray *array = [CIFilter filterNamesInCategory:filterCategory];
        NSLog(@"%@\n%@\n", filterCategory, array);
    }
    
    CIImage *inputImage = [[CIImage alloc] initWithImage:image];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:@(10.0) forKey:kCIInputRadiusKey];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];
    CGImageRef outputImageRef = [context createCGImage:outputImage fromRect:outputImage.extent];
    
    UIImage *effectImage = nil;
    if (outputImageRef) {
        effectImage = [UIImage imageWithCGImage:outputImageRef];
        CGImageRelease(outputImageRef);
    }
    
    return effectImage;
}

+ (UIImage *)gaussianBlurEffectForImage:(UIImage *)image
{
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CIImage *inputImage = [[CIImage alloc] initWithImage:image];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    
    // Setup the parameters for the gaussian blur filter
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:@(10.0) forKey:kCIInputRadiusKey];
    
    
    CIImage *outputImage = filter.outputImage;
    CGImageRef outputImageRef = [context createCGImage:outputImage fromRect:outputImage.extent];

    UIImage *effectImage = nil;
    if (outputImageRef) {
        effectImage = [UIImage imageWithCGImage:outputImageRef];
    }

    return effectImage;
}

@end
